const express = require('express');
const router = express.Router();
const ResultJson = require("../util/json")
const fs = require("fs")
const multer = require("multer")

router.post('/', multer({dest: "./temp"}).single("file"), function (req, res) {
    fs.readFile(req.file.path, function (e, d) {
        let ons = req.file.originalname.split(".");
        let fileType = ons[ons.length - 1];
        let fileName = req.file.filename + "." + fileType;
        fs.writeFile(`./public/upload/${fileName}`, d, function (e) {
            if (e) {
                throw e;
            }
            fs.unlinkSync("./temp/" + req.file.filename);//删除临时文件
            res.json(ResultJson.data(fileName))
        })
    })
})

module.exports = router;
